/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Dialog.java
 *
 * Created on 06/10/2011, 17:45:44
 */
package wf.ui.windows;

import bc.swing.pfrm.Model;
import bc.swing.pfrm.Page;
import bc.swing.pfrm.PageLayout;
import bc.swing.pfrm.layouts.FormLayout;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JPanel;

/**
 *
 * @author bennyl
 */
public class Dialog extends javax.swing.JDialog {

    public static final String CANCEL_ACTION = "CANCEL";
    public static final String OK_ACTION = "OK";
    boolean actionPerformed;
    Page page;

    /** Creates new form Dialog */
    public Dialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        gpainter = new org.jdesktop.swingx.painter.GlossPainter();
        bground = new javax.swing.JPanel();
        headerPanel = new org.jdesktop.swingx.JXPanel();
        caption = new javax.swing.JLabel();
        contentPan = new javax.swing.JPanel();
        footerPanel = new javax.swing.JPanel();
        okLink = new org.jdesktop.swingx.JXHyperlink();
        jXHyperlink2 = new org.jdesktop.swingx.JXHyperlink();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());

        bground.setBackground(new java.awt.Color(255, 255, 255));
        bground.setMinimumSize(new java.awt.Dimension(380, 105));
        bground.setLayout(new java.awt.GridBagLayout());

        headerPanel.setBackground(new java.awt.Color(186, 221, 255));
        headerPanel.setBackgroundPainter(gpainter);
        headerPanel.setPreferredSize(new java.awt.Dimension(100, 70));
        headerPanel.setLayout(new java.awt.GridBagLayout());

        caption.setFont(new java.awt.Font("Showcard Gothic", 0, 18));
        caption.setForeground(new java.awt.Color(102, 102, 102));
        caption.setText("Login");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        headerPanel.add(caption, gridBagConstraints);

        contentPan.setBackground(new java.awt.Color(186, 221, 255));
        contentPan.setOpaque(false);
        contentPan.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        headerPanel.add(contentPan, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 1.0;
        bground.add(headerPanel, gridBagConstraints);

        footerPanel.setBackground(new java.awt.Color(102, 102, 102));
        footerPanel.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createMatteBorder(3, 0, 3, 0, new java.awt.Color(186, 221, 255)), javax.swing.BorderFactory.createMatteBorder(1, 0, 1, 0, new java.awt.Color(255, 255, 255))));
        footerPanel.setMinimumSize(new java.awt.Dimension(0, 30));
        footerPanel.setPreferredSize(new java.awt.Dimension(0, 35));
        footerPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        okLink.setForeground(new java.awt.Color(255, 255, 255));
        okLink.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/img/ok16.png"))); // NOI18N
        okLink.setText("OK");
        okLink.setClickedColor(new java.awt.Color(255, 255, 255));
        okLink.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okLinkActionPerformed(evt);
            }
        });
        footerPanel.add(okLink);

        jXHyperlink2.setForeground(new java.awt.Color(255, 255, 255));
        jXHyperlink2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/img/cancel16.png"))); // NOI18N
        jXHyperlink2.setText("Cancel");
        jXHyperlink2.setClickedColor(new java.awt.Color(255, 255, 255));
        jXHyperlink2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXHyperlink2ActionPerformed(evt);
            }
        });
        footerPanel.add(jXHyperlink2);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        bground.add(footerPanel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(bground, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okLinkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okLinkActionPerformed
        actionPerformed = true;
        page.executeAction(OK_ACTION);
}//GEN-LAST:event_okLinkActionPerformed

    private void jXHyperlink2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXHyperlink2ActionPerformed
        actionPerformed = true;
        page.executeAction(CANCEL_ACTION);
}//GEN-LAST:event_jXHyperlink2ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        if (!actionPerformed) {
           page.executeAction(CANCEL_ACTION);
        }
    }//GEN-LAST:event_formWindowClosing
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bground;
    private javax.swing.JLabel caption;
    private javax.swing.JPanel contentPan;
    private javax.swing.JPanel footerPanel;
    private org.jdesktop.swingx.painter.GlossPainter gpainter;
    private org.jdesktop.swingx.JXPanel headerPanel;
    private org.jdesktop.swingx.JXHyperlink jXHyperlink2;
    private org.jdesktop.swingx.JXHyperlink okLink;
    // End of variables declaration//GEN-END:variables

    public void setPage(Page page) {
        this.page = page;
        final JPanel view = page.getView();

        if (view instanceof FormLayout) {
            ((FormLayout) view).setColorLabels(false);
        }

        contentPan.add(page.getView(), BorderLayout.CENTER);
        caption.setText("<HTML>" + page.getName().replace("\n", "<BR>") + "</HTML>");
        caption.setIcon(page.getIcon());
        page.addToDisposeList(new PageLayout() {

            @Override
            public void setPage(Page page) {
            }

            @Override
            public void onDispose() {
                setVisible(false);

            }
        });

        validate();
        repaint();
    }

    public static void show(Model m, int width, int height) {
        Dialog diag = new Dialog(null, true);
        Page p = Page.get(m);
        diag.setPage(p);
        diag.setMinimumSize(new Dimension(width, height));
        diag.setLocationRelativeTo(null);
        diag.pack();
        diag.setVisible(true);
    }
}
